<?php
$isReloadable = true;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.getFiles.php');

if (reqVar('azione')=='elimina') {
	$msgPag .= azioneElimina();
}
if (reqVar('azione')=='carica') {
	$msgPag .= azioneCarica();
}
if (reqVar('azione')=='installa') {
	$msgPag .= azioneInstalla();
}

$sql_sel = "SELECT m.cod_modulo, m.descr as m_descr "
		.", p.*, c.id_comando, c.cod_comando, c.descr AS c_descr, c.id_pagina_fin, c.stato_fin "
		.", ri.cod_regola AS reg_inp "
		.", rv.cod_regola AS reg_val "
		.", ro.cod_regola AS reg_out "
		.", s.descr AS s_descr, pf.cod_pagina AS pf_cod_pagina, pf.descr AS pf_descr "
		." FROM wfm_pagine p " 
		." LEFT JOIN wfm_moduli m ON p.id_modulo = m.id_modulo "
		." LEFT JOIN wfm_regole ri ON ri.id_regola = p.id_regola_inp "
		." LEFT JOIN wfm_regole rv ON rv.id_regola = p.id_regola_val "
		." LEFT JOIN wfm_regole ro ON ro.id_regola = p.id_regola_out "
		." LEFT JOIN wfm_comandi c ON c.id_pagina = p.id_pagina "
		." LEFT JOIN wfm_stati s ON s.id_modulo = p.id_modulo AND s.stato = c.stato_fin"
		." LEFT JOIN wfm_pagine pf ON pf.id_pagina = c.id_pagina_fin"
		." WHERE p.id_pagina = ".reqVarInt('id');
$objDB->query($sql_sel,'selRec');
if(!$aRow = $objDB->fetch('selRec')) {
	backOnErr("id. errato");
}
reqDef('cod_pagina',$aRow['cod_pagina']);
reqDef('descr',$aRow['descr']);
reqDef('id_regola_inp',$aRow['id_regola_inp']);
reqDef('id_regola_val',$aRow['id_regola_val']);
reqDef('id_regola_out',$aRow['id_regola_out']);
reqDef('tipo',$aRow['tipo']);
reqDef('tipo_inst',$aRow['tipo_inst']);
reqDef('nota',$aRow['nota']);
reqDef('idm',$aRow['id_modulo']);

$aCamForm = array();
require("_inc/const.campiPag.php");
if (reqVar('tipo')=='pers' || reqvar('nota')=='') {
	unset($aCamForm[90]);
} 
if (reqVar('tipo')!='pers') {
	unset($aCamForm[95]);
}

$sCmdL = "";
$sCmdL .= "<a class=\"cmd1\" href=\"pagScrView.php?id=".reqVarUE('id')."&$sXSNQS"."\" >script</a>";

$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"cmdIns.php?idp=".reqVarUE('id')."&$sXSNQS"."\" >aggiungi comando</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:confElim();\" >elimina</a>";
$sCmdR .= "&nbsp;&nbsp;&nbsp;&nbsp;";
$sCmdR .= "<a class=\"cmd1\" href=\"pagMod.php?id=".reqVarUE('id')."&$sXSNQS"."\" >modifica</a>";
$sCmdR .= cmdTorna('torna');

$sHtmlMod = "<table class=\"tab1\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"100%\" align='center'>\n";
$sHtmlMod .= getHtmlRigaRif();
$sHtmlMod .= "<tr>\n";
$sHtmlMod .= "<td class='lab1' colspan='3'>Modulo</td>\n";
$sHtmlMod .= "<td class='txt1' colspan='3'>".$aRow['cod_modulo']."</td>\n";
$sHtmlMod .= "<td class='txt1' colspan='14'>".$aRow['m_descr']."</td>\n";
$sHtmlMod .= "</tr>\n";
$sHtmlMod .= "</table>";

$aOpz['sottotit'] = $sHtmlMod;

testaHtml('Gestione work-flow','Visualizza pagina',$msgPag,'wfma',$aOpz);
?>
<script type="text/javascript">
<!--
function confElim() {
	if (confirm("Confermi l'eliminazione ?")) {
		document.location.href = "pagView.php?<?="id=".reqVarInt('id')."&azione=elimina&$sXSNQS"?>";
	} 
}
function confInst() {
	if (confirm("Confermi l'installazione ?")) {
		document.location.href = "pagView.php?<?="id=".reqVarInt('id')."&azione=installa&$sXSNQS"?>";
	} 
}
//-->
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('cod');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Pagina</td>
   </tr>
	<?
if (reqVar('id_regola_inp')) $aVarExt['hrefRegInp'] = "regView.php?id=".reqVar('id_regola_inp')."&xsn=".XSN;
if (reqVar('id_regola_val')) $aVarExt['hrefRegVal'] = "regView.php?id=".reqVar('id_regola_val')."&xsn=".XSN;
if (reqVar('id_regola_out')) $aVarExt['hrefRegOut'] = "regView.php?id=".reqVar('id_regola_out')."&xsn=".XSN;

echo getHtmlCampiForm($aCamForm,'vis',$aVarExt);
?>
   <tr>
      <td class="tit1" colspan='20' align='left'>Comandi</td>
   </tr>
   <tr>
      <td class="lab1" colspan='5' align='center'>Codice</td>
      <td class="lab1" colspan='5' align='center'>Descrizione</td>
      <td class="lab1" colspan='3' align='center'>Pagina fin.</td>
      <td class="lab1" colspan='7' align='center'>Stato fin.</td>
   </tr>
<?
$clRF = 'txt2';
if (!empty($aRow['id_comando'])) while($aRow) {
	$clRF = ($clRF=='txt1')?'txt2':'txt1';	
?>
   <tr>
      <td class="<?=$clRF?>" colspan='5' align='left'>
      	<a href='cmdView.php?id=<?=$aRow['id_comando']."&$sXSNQS"?>' class="<?=$clRF?>"
      	 	style='font-family:lucida console,courier;'><?echo $aRow['cod_comando'];?></a>
      </td>
      <td class="<?=$clRF?>" colspan='5' align='left'>
      	<?=$aRow['c_descr']?>
		</td>
      <td class="<?=$clRF?>" colspan='3' align='left'>
      	<a href='pagView.php?id=<?=$aRow['id_pagina_fin']."&$sXSNQS"?>' class="<?=$clRF?>"
      	 	style='font-family:lucida console,courier;'><?echo $aRow['pf_cod_pagina'];?></a>
      </td>
      <td class="<?=$clRF?>" colspan='7' align='left'>
      	<?=$aRow['stato_fin']." - ".$aRow['s_descr']?>
		</td>
   </tr>
<?
	$aRow = $objDB->fetch('selRec');
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
			<? echo $sCmdL; ?>
      </td>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();

function azioneElimina() {
	global $objDB;
	$msg = "";
	$sSql = "SELECT cod_modulo FROM wfm_moduli "
		." WHERE id_pagina_ini = ".reqVarInt('id')
		." OR id_pagina_vis = ".reqVarInt('id');
	$objDB->exec($sSql);
	$aX = $objDB->fetch();
	if ($aX) {
		$msg .= "pagina utilizzata come pagina iniz. o visualizz.del modulo ".$aX['cod_modulo']."<br>";
	}
	if ($msg != "") return $msg;
	
	$sSql = "SELECT cod_pagina FROM wfm_pagine WHERE id_pagina = ".reqVarInt('id');
	$objDB->exec($sSql);
	$aX = $objDB->fetch();
	
	$sSql = "DELETE FROM wfm_comandi WHERE id_pagina = ".reqVarInt('id');
	$objDB->exec($sSql);
	$sSql = "DELETE FROM wfm_pagine WHERE id_pagina = ".reqVarInt('id');
	$objDB->exec($sSql);
	header("location: ".urlTorna("Pagina '".$aX['cod_pagina']."' eliminata"));
	exit;
}

function azioneCarica() {
	global $objDB;
	$msg = "";
	if (!is_uploaded_file($_FILES['filecar']['tmp_name']))  {
		return "file non caricato";
	}
	$sql_sel = "UPDATE wfm_pagine SET script = '".addslashes(file_get_contents($_FILES['filecar']['tmp_name']))."' "
			." WHERE id_pagina = ".reqVarInt('id');
	$objDB->query($sql_sel,'selRec');
	return "pagina caricata";
}

function azioneInstalla() {
	global $objDB;
	$msg = "";
	$sSql = "SELECT m.cod_modulo, m.descr as m_descr "
			.", p.* "
			." FROM wfm_pagine p " 
			." LEFT JOIN wfm_moduli m ON p.id_modulo = m.id_modulo "
			." WHERE p.id_pagina = ".reqVarInt('id');
	$objDB->query($sSql,'selRec');
	if(!$aRow = $objDB->fetch('selRec')) {
		backOnErr("id. errato");
	}
	$pathPag = OVF_FS_WEB_ROOT."/pers/wfma/".$aRow['cod_modulo']
		."/pagina_".$aRow['cod_pagina'].".php";
	file_put_contents($pathPag,$aRow['script']);
	
	$sSql = "UPDATE wfm_pagine SET tipo_inst = tipo "
			." WHERE id_pagina = ".reqVarInt('id');
	$objDB->query($sSql,'selRec');
	
	return "pagina installata";
}
